package com.maserb.monitor.dataCenter.communication;

import lombok.Data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Data
public class WaveNetPackage {
    private String deviceCode;
    private String pointCode;
    private float sampleFrequence;
    private SystemTime waveBeginTime;
    private Integer waveDataLength;
    private SystemTime eigenValueTime;
    private Integer eigenValueCount;
    private List<EigenValue> eigenValueList;
    private float[] waveData;

    public static WaveNetPackage convert(byte[] content) {
        WaveNetPackage p = new WaveNetPackage();
        Box<Integer> index = new Box<Integer>(0);
        Integer count = 8;
        p.deviceCode = ByteUtils.getString(content, index, count);
        p.pointCode = ByteUtils.getString(content, index, count);
        p.sampleFrequence = ByteUtils.getFloat(content, index);
        p.waveBeginTime = SystemTime.convert(content, index);
        p.waveDataLength = ByteUtils.getInt(content, index);
        p.eigenValueTime = SystemTime.convert(content, index);
        p.eigenValueCount = ByteUtils.getInt(content, index);

        p.eigenValueList = new LinkedList<EigenValue>();
        for (int i = 0; i < p.eigenValueCount; i++) {
            EigenValue eigenValue = EigenValue.convert(content, index);
            p.eigenValueList.add(eigenValue);
        }

        try {
            p.waveData = new float[p.waveDataLength];
            for (int i = 0; i < p.waveDataLength; i++) {
                float pData = ByteUtils.getFloat(content, index);
                p.waveData[i] = pData;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return p;
    }
}
